
<!--话术管理，大体逻辑、函数、变量与用户管理页面中相同-->

<template>
  <div>
    <el-card id="search">
      <el-input v-model="searchModel.keyword" placeholder="请输入关键字"></el-input>
      <el-button type="success" @click="">查询</el-button>
      <el-button type="primary" @click="openEditUI(null)">新增</el-button>

      <el-dropdown style="float: right">
        <el-button type="primary">
          学院
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item datatype="1" @click.native="collegeList($event)">计算机与软件学院</el-dropdown-item>
          <el-dropdown-item datatype="2" @click.native="collegeList($event)">物理与光电工程学院</el-dropdown-item>
          <el-dropdown-item datatype="3" @click.native="collegeList($event)">机电与控制工程学院</el-dropdown-item>
          <el-dropdown-item datatype="4" @click.native="collegeList($event)">土木与交通工程学院</el-dropdown-item>
          <el-dropdown-item datatype="5" @click.native="collegeList($event)">电子与信息工程学院</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-card>

    <el-card style="margin-top: 30px">
      <!--      话术表格-->
      <el-table :data="scriptList" :height="tableHeight" style="width: 100%">
        <el-table-column prop="cid" label="序号" width="100" align="center" header-align="center" type="index"></el-table-column>
        <el-table-column prop="csid" label="学院" width="330" align="center" header-align="center"></el-table-column>
        <el-table-column prop="ckey" label="关键字" width="200" align="center" header-align="center"></el-table-column>
        <el-table-column prop="value" label="内容" width="500" align="center" header-align="center"></el-table-column>
        <el-table-column label="操作" width="150" align="center" header-align="center">
          <template slot-scope="scope">
            <el-button type="default" size="small" @click="openEditUI(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="deleteScript(scope.row.cid)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--      分页器和页码选择器-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchModel.pageNo"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="searchModel.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 15px"
      ></el-pagination>
    </el-card>

    <!--    弹窗-->
    <el-dialog :visible.sync="dialogFormVisible" :title="title" center @close="closeEditUI">
      <el-form :model="scriptForm">
        <el-form-item label="所在学院" label-width="100px">
          <!--            <el-input v-model="scriptForm.college" autocomplete="off" placeholder="请输入学院名称"/>-->
          <el-select v-model="value" placeholder="请选择" clearable filterable @blur="selectBlur" @clear="selectClear" @change="selectChange">
            <el-option v-for="(item, index) in options" :key="index" :label="item.label" :value="item.value" @click.native="labelClick(item.label)" />
          </el-select>
        </el-form-item>
        <el-form-item label="话术关键字" label-width="100px">
          <el-input v-model="scriptForm.keyword" autocomplete="off" placeholder="请输入关键字" />
        </el-form-item>
        <el-form-item label="话术内容" label-width="100px">
          <!-- <el-input v-model="scriptForm.content" autocomplete="off" type="textarea" autosize="{ minRows: 1, maxRows: 10 }" placeholder="请输入话术内容"/>
             -->
        </el-form-item>
      </el-form>
      <div>
        <quill-editor v-model="content"></quill-editor>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button
            type="primary"
            @click="
              dialogFormVisible = false
              addScript()
            "
          >
            确定
          </el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<!-- Include the Quill library -->
<script src="https://cdn.quilljs.com/1.3.4/quill.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- Quill JS Vue -->
<script src="https://cdn.jsdelivr.net/npm/vue-quill-editor@3.0.6/dist/vue-quill-editor.js"></script>
<!-- Include stylesheet -->
<link href="https://cdn.quilljs.com/1.3.4/quill.core.css" rel="stylesheet"></link>
<link href="https://cdn.quilljs.com/1.3.4/quill.snow.css" rel="stylesheet"></link>
<link href="https://cdn.quilljs.com/1.3.4/quill.bubble.css" rel="stylesheet"></link>

  <script>
import scriptApi from '@/api/script'
import { getToken } from '@/utils/auth'
import VueQuillEditor from 'vue-quill-editor'
export default {
   
  name: 'script',
  components: {
    quillEditor: VueQuillEditor.default,
  },
  data() {
    return {
      content: '',
      type: '',
      record_type: '',
      temp_cid: '',
      title: '新增话术',
      tableHeight: '',
      collegeName: '',
      dialogFormVisible: false,

      total: 0,
      searchModel: {
        keyword: '',
        pageNo: 1,
        pageSize: 10,
      },

      scriptList: [],
      //暂存列表
      tempList: [],
      tempDelList: [],
      // {
      //   No: 1,
      //   college: '计算机与软件学院',
      //   keyword: '学费',
      //   content: '学费在哪里缴纳？',
      // },
      // {
      //   No: 2,
      //   college: '土木与交通工程学院',
      //   keyword: '院楼',
      //   content: '土木院楼怎么走？',
      // }
      scriptForm: {
        college: '',
        keyword: '',
        content: '',
      },
      value: '',
      options: [
        { value: '计算机与软件学院', label: '计算机与软件学院' },
        { value: '物理与光电工程学院', label: '物理与光电工程学院' },
        { value: '机电与控制工程学院', label: '机电与控制工程学院' },
        { value: '土木与交通工程学院', label: '土木与交通工程学院' },
        { value: '电子与信息工程学院', label: '电子与信息工程学院' },
      ],
    }
  },
  created() {},
  methods: {
    handleSizeChange(val) {},
    handleCurrentChange(val) {},
    openEditUI(val) {
      if (val === null) {
        this.title = '新增话术'
      } else {
        this.title = '修改话术'
        this.scriptForm = {
          keyword: val.keyword,
          content: val.content,
        }
      }
      this.dialogFormVisible = true
    },
    closeEditUI() {
      this.dialogFormVisible = false
      this.scriptForm = {
        keyword: '',
        content: '',
      }
    },
    getScriptList(val) {
      scriptApi
        .getScriptList({
          token: getToken(),
          cid: val,
          pageNo: this.searchModel.pageNo,
          pageSize: this.searchModel.pageSize,
        })
        .then((response) => {
          this.tempList = response.data.rows
          this.tempDelList = JSON.parse(JSON.stringify(this.tempList))
          for (let i in this.tempList) {
            this.tempList[i].cid = parseInt(i) + 1
            this.tempList[i].csid = this.collegeName
          }
          this.total = response.data.total
          this.scriptList = this.tempList
        })
    },
    getCollegeName() {
      scriptApi
        .getCollegeName({
          cid: this.type,
        })
        .then((response) => {
          this.collegeName = response.data[0].college
        })
    },
    collegeList(e) {
      this.type = parseInt(e.target.getAttribute('datatype'))
      this.getCollegeName()
      this.getScriptList(this.type)
    },
    selectBlur(e) {
      // 意见类型
      if (e.target.value !== '') {
        this.value = e.target.value + '(其他)'
        this.$forceUpdate() // 强制更新
      }
    },
    selectClear() {
      this.value = ''
      this.$forceUpdate()
    },
    selectChange(val) {
      this.value = val
      this.$forceUpdate()
    },
    labelClick(data) {
      if (data.includes('计')) {
        this.record_type = 1
      } else if (data.includes('物')) {
        this.record_type = 2
      } else if (data.includes('机')) {
        this.record_type = 3
      } else if (data.includes('土')) {
        this.record_type = 4
      } else if (data.includes('信')) {
        this.record_type = 5
      }
      console.log(this.record_type)
    },
    addScript() {
      scriptApi
        .addScript({
          token: getToken(),
          cid: this.record_type,
          keyword: this.scriptForm.keyword,
          content: this.scriptForm.content,
        })
        .then((response) => {
          console.log('success')
        })
    },
    deleteScript(cid) {
      // console.log(cid)
      this.$confirm('此操作将删除该话术，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        // console.log(this.tempDelList[cid])
        scriptApi
          .deleteScript({
            token: getToken(),
            cid: cid,
            csid: this.tempDelList[cid].csid,
          })
          .then((response) => {
            this.$message.success('删除成功')
            this.scriptList()
          })
      })
    },
  },
}
</script>

  <style scoped>
#search .el-input {
  width: 200px;
  margin-right: 20px;
}
#search .el-dropdown {
  margin-right: 20px;
}
</style>
